var add_win;
var edit_win;

$(function() {
	$(".context").height($(window).height() - 100);
	$("#dept-tree").tree({
		url: "system/Department/getTree",
		onClick : function(node, bb) {
			$(".easyui-simplegrid").simplegrid({
				url: "system/Department/get",
				data : {dept_suos : node.id}
			});
		}
	});
});

/**
 * 添加科室信息
 */
function btn_add() {
	var now = $("#dept-tree").tree("getSelected");
	if (now == null || now.level == undefined || now.level.length == 0) {
		alert("请在医院或者一级科室下添加");
		return;
	} else if (now.level == -1) {
		alert("请选择对应的医院");
		return;
	} else if (now.level == 3) {
		alert("已为2级科室，请在医院或者一级科室下添加");
		return;
	}
	$("#department-button").unbind();
	$("#department-button").click(exec_add);
	$("#department-form")[0].reset();
	if (now.level == 1) {
		$("#department-form #yiybh").val(now.id);
		$("#department-form #cengc").val(1);
	} else if (now.level == 2) {
		$("#department-form #yiybh").val(now.yiybh);
		$("#department-form #suos").val(now.id);
		$("#department-form #cengc").val(2);
	}
	$("#department-div").window({title: '添加部门', iconCls:'icon-add', closed: false});
}

function exec_add() {
	data = $("#department-form").serializeArray();
	$.ajax({
		url: 'system/Department/add',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if (data.result > 0) {
				$('#department-form')[0].reset();
				$('#department-div').window('close');
				$("#dept-tree").tree();
				$(".easyui-simplegrid").simplegrid("refresh");
				alert("添加成功！");
			} else {
				this.error();
			}
		},
		error: function() {
			alert("添加失败");
		}
	});
}

/** 编辑科室信息 */
function btn_edit() {
	var dept = $(".easyui-simplegrid").simplegrid("getSelected");
	if (dept.length == 0) {
		alert("当前您没有选择!"); 
		return;
	}
	if (dept.length > 2) {
		alert("请选择单条记录!");
		return;
	}
	dept = dept[0];
	$("#department-button").unbind();
	$("#department-form").form("load", {
		'department.bh': dept.bh,
		'department.mingc': dept.mingc,
		'department.suos': dept.suos,
		'department.yiybh': dept.yiybh
	});
	$("#department-button").click(exec_edit);
	$("#department-div").window({title: '编辑部门', iconCls:'icon-edit', closed: false});
}


function exec_edit() {
	var data = $("#department-form").serializeArray();
	$.ajax({
		url: 'system/Department/edit',
		type: 'post',
		dataType: 'json',
		data: data,
		success: function(data) {
			if(data.result > 0) {
				$('#department-form')[0].reset();
				$('#department-div').window('close');
				alert("编辑成功");
				$("#dept-tree").tree();
				$(".easyui-simplegrid").simplegrid();
			} else {
				this.error();
			}
		},
		error: function() {
			alert("编辑失败");
		}
	});

}

/** 删除科室信息 */
function btn_delete() {
	var dept = $(".easyui-simplegrid").simplegrid("getSelected");
	if (dept.length == 0) {
		alert("当前您没有选择!"); 
		return;
	}
	if (dept.length > 2) {
		alert("请选择单条记录!");
		return;
	}
	dept = dept[0];
	if (dept.haschildren > 0){
		alert("该部门存在下级科室，请先删除下级部门!");
		return;
	}
	if (dept.hasgroup > 0){
		alert("该部门已经绑定数据组，请先删除数据组!");
		return;
	}
	if (confirm("确认删除[" + dept.mingc + "]吗?")) {
		$.post("system/Department/del?bh="+dept.bh, function(data) {
			if (data.result > 0) {
				alert("删除成功！");
				$(".easyui-simplegrid").simplegrid("clear");
				$("#dept-tree").tree();
			} else {
				alert('删除失败!');
			}
		}, "json");
	}
}
